﻿
using EpubFactory.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EpubFactory.Robot.UI
{
    public partial class OptionForm : Form
    {
        public OptionForm()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var preference = AppPreference.Load();
            var dir = txtTempDir.Text;
            if (!string.IsNullOrWhiteSpace(dir))
            {
                if (CheckDirValid(dir))
                {
                    preference.TempDir = dir;
                }
                else
                {
                    MessageBox.Show("临时目录无效或者无权限");
                    return;
                }
            }
            dir = txtOutputDir.Text;
            if (!string.IsNullOrWhiteSpace(dir))
            {
                if (CheckDirValid(dir))
                {
                    preference.OutputDir = dir;
                }
                else
                {
                    MessageBox.Show("输出目录无效或者无权限");
                    return;
                }
            }

            preference.ParallelismDegree = Int32.Parse(txtParallelismDegree.Text);

            BookFormat format = BookFormat.None;

            if (cbEpub.Checked)
            {
                format = format | BookFormat.Epub201;
            }
            if (cbMobi.Checked)
            {
                format = format | BookFormat.Mobi;
            }
            if (cbText.Checked)
            {
                format = format | BookFormat.PlainText;
            }
            if (cbEpub3.Checked)
            {
                format |= BookFormat.Epub310;
            }
            preference.OutputFormatters = format;
            preference.EnableRequestCache = cbCacheRequest.Checked;
            AppPreference.Save();
            //Notification.Show("保存成功");
        }
        private bool CheckDirValid(string path)
        {
            try
            {
                Directory.CreateDirectory(path);
            }
            catch (UnauthorizedAccessException)
            {
                return false;
            }
            catch (DirectoryNotFoundException)
            {
                return false;
            }

            string filePath = Path.Combine(path, "a.txt");
            try
            {
                using (var stream = File.Create(filePath))
                {
                    stream.Write(new byte[] { 12, 20 }, 0, 2);
                }
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
            catch (UnauthorizedAccessException)
            {
                return false;
            }
            return true;
        }

        private void btnChangeOutputDir_Click(object sender, EventArgs e)
        {
            folderDialog.ShowDialog();
            if (!string.IsNullOrWhiteSpace(folderDialog.SelectedPath))
            {
                txtOutputDir.Text = folderDialog.SelectedPath;
            }
        }

        private void btnChangeTempDir_Click(object sender, EventArgs e)
        {
            folderDialog.ShowDialog();
            if (!string.IsNullOrWhiteSpace(folderDialog.SelectedPath))
            {
                txtTempDir.Text = folderDialog.SelectedPath;
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtOutputDir.Text = AppPreference.DefaultOutputDir;
            txtTempDir.Text = AppPreference.DefaultTempDir;
            txtParallelismDegree.Text = "0";
            cbEpub.Checked = false;
            cbEpub3.Checked = true;
            cbMobi.Checked = false;
            cbText.Checked = false;
        }

        private void OptionForm_Load(object sender, EventArgs e)
        {
            var preference = AppPreference.Load();
            txtTempDir.Text = preference.TempDir;
            txtOutputDir.Text = preference.OutputDir;
            txtParallelismDegree.Text = preference.ParallelismDegree.ToString();
            cbEpub.Checked = preference.OutputFormatters.HasFlag(BookFormat.Epub201);
            cbEpub3.Checked = preference.OutputFormatters.HasFlag(BookFormat.Epub310);
            cbText.Checked = preference.OutputFormatters.HasFlag(BookFormat.PlainText);
            cbMobi.Checked = preference.OutputFormatters.HasFlag(BookFormat.Mobi);
            cbCacheRequest.Checked = preference.EnableRequestCache;
        }
    }
}
